namespace ASPNET.StarterKit.IssueTracker.BusinessLogicLayer
{
    using ASPNET.StarterKit.IssueTracker.DataAccessLayer;
    using System;

    public class Issue
    {
        private string _AssignedDisplayName;
        private int _AssignedId;
        private int _CategoryId;
        private string _CategoryName;
        private string _CreatorDisplayName;
        private int _CreatorId;
        private string _CreatorUsername;
        private DateTime _DateCreated;
        private int _Id;
        private int _MilestoneId;
        private string _MilestoneImageUrl;
        private string _MilestoneName;
        private string _OwnerDisplayName;
        private int _OwnerId;
        private int _PriorityId;
        private string _PriorityImageUrl;
        private string _PriorityName;
        private int _ProjectId;
        private int _StatusId;
        private string _StatusImageUrl;
        private string _StatusName;
        private string _Title;

        public Issue(int id, int projectId, string title, int categoryId, int milestoneId, int priorityId, int statusId, int assignedId, int ownerId, string creatorUsername) : this(id, projectId, title, categoryId, string.Empty, milestoneId, string.Empty, string.Empty, priorityId, string.Empty, string.Empty, statusId, string.Empty, string.Empty, string.Empty, assignedId, string.Empty, ownerId, creatorUsername, DefaultValues.GetUserIdMinValue(), creatorUsername, DefaultValues.GetDateTimeMinValue())
        {
        }

        public Issue(int id, int projectId, string title, int categoryId, string categoryName, int milestoneId, string milestoneName, string milestoneImageUrl, int priorityId, string priorityName, string priorityImageUrl, int statusId, string statusName, string statusImageUrl, string assignedDisplayName, int assignedId, string ownerDisplayName, int ownerId, string creatorDisplayName, int creatorId, string creatorUsername, DateTime dateCreated)
        {
            this._Id = id;
            this._ProjectId = projectId;
            this._Title = title;
            this._CategoryId = categoryId;
            this._CategoryName = categoryName;
            this._MilestoneId = milestoneId;
            this._MilestoneName = milestoneName;
            this._MilestoneImageUrl = milestoneImageUrl;
            this._PriorityId = priorityId;
            this._PriorityName = priorityName;
            this._PriorityImageUrl = priorityImageUrl;
            this._StatusId = statusId;
            this._StatusName = statusName;
            this._StatusImageUrl = statusImageUrl;
            this._DateCreated = dateCreated;
            this._AssignedDisplayName = assignedDisplayName;
            this._AssignedId = assignedId;
            this._OwnerDisplayName = ownerDisplayName;
            this._OwnerId = ownerId;
            this._CreatorDisplayName = creatorDisplayName;
            this._CreatorUsername = creatorUsername;
            this._CreatorId = creatorId;
        }

        public static bool DeleteIssue(int issueId)
        {
            if (issueId <= DefaultValues.GetIssueIdMinValue())
            {
                throw new ArgumentOutOfRangeException("issueId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().DeleteIssue(issueId);
        }

        public static Issue GetIssueById(int issueId)
        {
            if (issueId <= DefaultValues.GetIssueIdMinValue())
            {
                throw new ArgumentOutOfRangeException("issueId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetIssueById(issueId);
        }

        public static IssueCollection GetIssuesByAssignedUsername(int projectId, string username)
        {
            if (projectId <= DefaultValues.GetProjectIdMinValue())
            {
                throw new ArgumentOutOfRangeException("projectId");
            }
            if ((username == null) || (username.Length == 0))
            {
                throw new ArgumentOutOfRangeException("username");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetIssuesByAssignedUsername(projectId, username);
        }

        public static IssueCollection GetIssuesByCreatorUsername(int projectId, string username)
        {
            if (projectId <= DefaultValues.GetProjectIdMinValue())
            {
                throw new ArgumentOutOfRangeException("projectId");
            }
            if ((username == null) || (username.Length == 0))
            {
                throw new ArgumentOutOfRangeException("username");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetIssuesByCreatorUsername(projectId, username);
        }

        public static IssueCollection GetIssuesByOwnerUsername(int projectId, string username)
        {
            if (projectId <= DefaultValues.GetProjectIdMinValue())
            {
                throw new ArgumentOutOfRangeException("projectId");
            }
            if ((username == null) || (username.Length == 0))
            {
                throw new ArgumentOutOfRangeException("username");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetIssuesByOwnerUsername(projectId, username);
        }

        public static IssueCollection GetIssuesByProjectId(int projectId)
        {
            if (projectId <= DefaultValues.GetProjectIdMinValue())
            {
                throw new ArgumentOutOfRangeException("projectId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetIssuesByProjectId(projectId);
        }

        public static IssueCollection GetIssuesByRelevancy(int projectId, string username)
        {
            if (projectId <= DefaultValues.GetProjectIdMinValue())
            {
                throw new ArgumentOutOfRangeException("projectId");
            }
            if ((username == null) || (username.Length == 0))
            {
                throw new ArgumentOutOfRangeException("username");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetIssuesByRelevancy(projectId, username);
        }

        public static IssueCollection PerformQuery(int projectId, QueryClauseCollection queryClauses)
        {
            if (queryClauses.Count == 0)
            {
                throw new ArgumentOutOfRangeException("queryClauses");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().PerformQuery(projectId, queryClauses);
        }

        public static IssueCollection PerformSavedQuery(int projectId, int queryId)
        {
            if (queryId <= DefaultValues.GetQueryIdMinValue())
            {
                throw new ArgumentOutOfRangeException("queryId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().PerformSavedQuery(projectId, queryId);
        }

        public bool Save()
        {
            DataAccessLayerBaseClass DBLayer = DataAccessLayerBaseClassHelper.GetDataAccessLayer();
            if (this.Id <= DefaultValues.GetIssueIdMinValue())
            {
                int TempId = DBLayer.CreateNewIssue(this);
                if (TempId > 0)
                {
                    this._Id = TempId;
                    return true;
                }
                return false;
            }
            bool result = DBLayer.UpdateIssue(this);
            IssueNotification.SendIssueNotifications(this.Id);
            return result;
        }

        public string AssignedDisplayName
        {
            get
            {
                if ((this._AssignedDisplayName == null) || (this._AssignedDisplayName.Length == 0))
                {
                    return string.Empty;
                }
                return this._AssignedDisplayName;
            }
            set
            {
                this._AssignedDisplayName = value;
            }
        }

        public int AssignedId
        {
            get
            {
                return this._AssignedId;
            }
        }

        public int CategoryId
        {
            get
            {
                return this._CategoryId;
            }
            set
            {
                this._CategoryId = value;
            }
        }

        public string CategoryName
        {
            get
            {
                return this._CategoryName;
            }
            set
            {
                this._CategoryName = value;
            }
        }

        public string CreatorDisplayName
        {
            get
            {
                return this._CreatorDisplayName;
            }
        }

        public int CreatorId
        {
            get
            {
                return this._CreatorId;
            }
        }

        public string CreatorUsername
        {
            get
            {
                return this._CreatorUsername;
            }
        }

        public DateTime DateCreated
        {
            get
            {
                return this._DateCreated;
            }
        }

        public int Id
        {
            get
            {
                return this._Id;
            }
        }

        public int MilestoneId
        {
            get
            {
                return this._MilestoneId;
            }
            set
            {
                this._MilestoneId = value;
            }
        }

        public string MilestoneImageUrl
        {
            get
            {
                return this._MilestoneImageUrl;
            }
        }

        public string MilestoneName
        {
            get
            {
                return this._MilestoneName;
            }
        }

        public string OwnerDisplayName
        {
            get
            {
                return this._OwnerDisplayName;
            }
        }

        public int OwnerId
        {
            get
            {
                return this._OwnerId;
            }
        }

        public int PriorityId
        {
            get
            {
                return this._PriorityId;
            }
            set
            {
                this._PriorityId = value;
            }
        }

        public string PriorityImageUrl
        {
            get
            {
                return this._PriorityImageUrl;
            }
        }

        public string PriorityName
        {
            get
            {
                return this._PriorityName;
            }
        }

        public int ProjectId
        {
            get
            {
                return this._ProjectId;
            }
        }

        public int StatusId
        {
            get
            {
                return this._StatusId;
            }
            set
            {
                this._StatusId = value;
            }
        }

        public string StatusImageUrl
        {
            get
            {
                return this._StatusImageUrl;
            }
        }

        public string StatusName
        {
            get
            {
                return this._StatusName;
            }
        }

        public string Title
        {
            get
            {
                if ((this._Title == null) || (this._Title.Length == 0))
                {
                    return string.Empty;
                }
                return this._Title;
            }
            set
            {
                this._Title = value;
            }
        }
    }
}

